//
//  MarkCell.m
//  QoilPublic
//
//  Created by SunChao on 17/9/9.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "MarkCell.h"
//#import "MarkView.h"
@interface MarkCell ()
@property (nonatomic,strong)NSMutableArray *dataSource;


@end
@implementation MarkCell
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    UILabel *topLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 22, kScreenWidth, 30)];
//    topLabel.text = @"评价";
    topLabel.textAlignment = NSTextAlignmentLeft;
    topLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:topLabel];
    NSArray *arr = @[@"快速准时",@"服务热情",@"性价比高"];
    NSInteger x = 30;
    NSInteger y = 40;
    
    for (NSInteger i=0; i<arr.count; i++) {
        NSString *str = [arr objectAtIndex:i];
//        CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]}];
//
//        if (x + size.width > self.frame.size.width) {
//            x = 20;
//            y = y + size.height+10; //5为两行之间的高度间隔
//        }
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 80, 30)];
        [button setTitle:str forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [button setBackgroundColor:[UIColor lightGrayColor]];
        [button setBackgroundImage:[self createImageWithColor:[UIColor colorWithRed:37.0/255.0 green:156.0/255.0 blue:210.0/255.0 alpha:1.0f]] forState:UIControlStateSelected];
        [button  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        button.layer.cornerRadius = 5;
//        button.layer.masksToBounds = YES;
        button.userInteractionEnabled = YES;
        [button addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
       
        [self addSubview:button];
        
        x = x + 80 +10; //10为两个标签之间的宽度间隔
    }

    
}

- (void)BtnAction:(UIButton *)button{
    
    NSLog(@"====%@",button.titleLabel.text);//button的tag
    NSString *selectedStr = button.titleLabel.text;
    if (button.selected ==YES) {
        [button setBackgroundColor:[UIColor lightGrayColor]];
        button.selected = NO;
        if (self.dataSource.count>0) {
           
            for (int i = 0; i<self.dataSource.count; i++) {
                NSString *str = [self.dataSource objectAtIndex:i];
                if ([str isEqualToString:selectedStr]) {
                    [self.dataSource removeObjectAtIndex:i];
                }
            }
        }
    }else{
        [button setBackgroundColor:[UIColor greenColor]];
        button.selected = YES;
        [self.dataSource addObject:button.titleLabel.text];
    }
    
    if(self.block){
        self.block(self.dataSource);
    }
}

-(UIImage*) createImageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
